<template>
  <div v-loading="loading" class="app-container " style="margin-top: 28px;">
    <div class="clearfix">
      <el-button size="mini" style="float: right" @click="mark_as_read('')">
        全部标记为已读
      </el-button>
    </div>
    <el-card v-for="item in data" :key="item.id" class="box-card">
      <div>
        <el-tag :type="item.unread ? 'info': 'success'">{{ item.unread ? '未读': '已读' }}</el-tag>
        <i style="color: red">{{ item.actor }}</i>
        {{ item.verb }}
        <div style="display: inline-block;float: right;">
          <i class="el-icon-date">{{ item.timestamp.split('T')[0] }}</i>
          <i class="el-icon-time">{{ item.timestamp.split('T')[1].slice(0, 8) }}</i>
          <el-button type="primary" size="mini" style="margin-left: 10px" @click="showDesc(item)">详情
          </el-button>
        </div>
      </div>
    </el-card>

    <el-pagination
      v-if="total"
      background
      class="mt-pagination"
      :current-page="page"
      :page-sizes="[10, 20,50,100]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handlePageChange"
    />
  </div>
</template>

<script>
import { GetAllNotificationList, MarkReadNotification, view_notification_description } from '@/api/notification'

export default {
  name: 'Notification',
  data() {
    return {
      loading: false,
      data: [],
      total: 0,
      page: 1,
      pagesize: 10
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      GetAllNotificationList(`page=${this.page}&pagesize=${this.pagesize}`)
        .then(response => {
          this.data = response.data.data
          this.total = response.data.total
          this.loading = false
        }).catch(e => {
          this.loading = false
        })
    },
    mark_as_read_all() {

    },
    showDesc(item) {
      this.loading = true
      view_notification_description(item.id)
        .then(response => {
          this.loading = false
          this.$alert(response.data.description, response.data.verb, {
            dangerouslyUseHTMLString: true
          }).then(() => {
            if (item.unread) {
              this.mark_as_read(item.id)
            }
          }).catch(e => {
          })
        })
        .catch(e => {
          this.loading = false
        })
    },
    mark_as_read(v) {
      MarkReadNotification(v).then(response => {
        this.page = 1
        this.getList()
      })
    },
    // pageSize 改变后触发 table 更新
    handleSizeChange(val) {
      this.pagesize = val
      this.getList()
    },
    // page 改变后触发 table 更新
    handlePageChange(val) {
      this.page = val
      this.getList()
    }
  }
}
</script>

<style scoped>
  .mt-pagination {
    float: right;
    margin-top: 5px;
  }

  .clearfix:after{
    content:"";/*设置内容为空*/
    height:0;/*高度为0*/
    display:block;/*将文本转为块级元素*/
    clear:both;/*清除浮动*/
  }
  .clearfix{
    zoom:1;/*为了兼容IE*/
    margin-bottom: 4px;
    margin-right: 4px;
  }

</style>
